package myapps.ttclient.activity;

import myapps.ttclient.GlobalVariables;
import myapps.ttclient.Helper;
import myapps.ttclient.R;
import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.protocol.message.MouseClickMessage;
import myapps.ttclient.protocol.message.MouseMoveMessage;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MouseControllerActivity extends Activity 
implements OnTouchListener, OnCheckedChangeListener {

	private TTClientApplication application;
	
	private ToggleButton tbMouseLeft;
	private ToggleButton tbMouseRight;

	private ImageView imgvTouchPad;

	private Button btnMouseLeft;
	private Button btnMouseMiddle;
	private Button btnMouseRight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		Helper.updateFullscreenStatus(this, true);
		
		//Helper.keepScreenOn(this);
		
		this.application = (TTClientApplication) this.getApplication();

		setContentView(R.layout.troubleshooting);
		
		tbMouseLeft = (ToggleButton) findViewById(R.id.tbMouseLeft);
		tbMouseLeft.setOnCheckedChangeListener(this);
		
		tbMouseRight = (ToggleButton) findViewById(R.id.tbMouseRight);
		tbMouseRight.setOnCheckedChangeListener(this);

		imgvTouchPad = (ImageView) findViewById(R.id.imageviewTouchPad);
		imgvTouchPad.setOnTouchListener(this);

		btnMouseLeft = (Button) findViewById(R.id.btnMouseLeft);
		btnMouseLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				uncheckAll();
				
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_LEFT, MouseClickMessage.STATE_DOWNUP));

			}
		});
		
		btnMouseMiddle = (Button) findViewById(R.id.btnMouseMiddle);
		btnMouseMiddle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				uncheckAll();
				
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_MIDDLE, MouseClickMessage.STATE_DOWNUP));

			}
		});

		btnMouseRight = (Button) findViewById(R.id.btnMouseRight);
		btnMouseRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				uncheckAll();
				
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_RIGHT, MouseClickMessage.STATE_DOWNUP));

			}
		});
	}
	
	@Override
	protected void onStop() {
		this.uncheckAll();
		super.onStop();
	}

	private int oldX, oldY, dx, dy;
	private int pointerID;

	private Boolean bIsAnUpDown;
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v == imgvTouchPad) {
			int action = event.getAction();
			int currentXPosition = (int) event.getX();
			int currentYPosition = (int) event.getY();

			if(action == MotionEvent.ACTION_DOWN) {
				oldX = currentXPosition;
				oldY = currentYPosition;
				
				bIsAnUpDown = true;
				
				//pointerID = event.getPointerId(0);
			} else if(action == MotionEvent.ACTION_MOVE) {
				dx = currentXPosition - oldX;
				dy = currentYPosition - oldY;
				oldX = currentXPosition;
				oldY = currentYPosition;

				bIsAnUpDown = false;
				//Log.d("myXY", String.valueOf(dx) + " ; " + String.valueOf(dy));
				
				this.application.sendMessage(
						new MouseMoveMessage(GlobalVariables.getTemporaryKey(), 
								dx, dy));
//				int pointerIndex = event.findPointerIndex(pointerID);
//				
//				int historySize = event.getHistorySize();
//		 	    for (int h = 0; h < historySize; h++) {
//		 	    	currentXPosition = (int) event.getHistoricalX(pointerIndex, h);
//					currentYPosition = (int) event.getHistoricalY(pointerIndex, h);
//					
//					dx = currentXPosition - oldX;
//					dy = currentYPosition - oldY;
//					oldX = currentXPosition;
//					oldY = currentYPosition;
//					
//					Log.d("myXY", String.valueOf(dx) + " ; " + String.valueOf(dy));
//
//					this.application.sendMessage(
//							new MouseMoveMessage(GlobalVariables.getTemporaryKey(), 
//									dx, dy));
//			     }
			} else if(action == MotionEvent.ACTION_UP) {
				dx = currentXPosition - oldX;
				dy = currentYPosition - oldY;
				oldX = currentXPosition;
				oldY = currentYPosition;

				this.application.sendMessage(
						new MouseMoveMessage(GlobalVariables.getTemporaryKey(), 
								dx, dy));
				
				if(bIsAnUpDown == true) {
					uncheckAll();
					
					MouseControllerActivity.this.application
					.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
							MouseClickMessage.BUTTON_LEFT, MouseClickMessage.STATE_DOWNUP));
				}
			}

			return true;
		}
		
		return false;
	}
	
	public void printSamples(MotionEvent ev) {
	     final int historySize = ev.getHistorySize();
	     final int pointerCount = ev.getPointerCount();
 	     for (int h = 0; h < historySize; h++) {
	         System.out.printf("At time %d:", ev.getHistoricalEventTime(h));
	         for (int p = 0; p < pointerCount; p++) {
	             System.out.printf("  pointer %d: (%f,%f)",
	                 ev.getPointerId(p), ev.getHistoricalX(p, h), ev.getHistoricalY(p, h));
	         }
	     }
	     System.out.printf("At time %d:", ev.getEventTime());
	     for (int p = 0; p < pointerCount; p++) {
	         System.out.printf("  pointer %d: (%f,%f)",
	             ev.getPointerId(p), ev.getX(p), ev.getY(p));
	     }
	 }
	
	private void uncheckAll() {
		if(tbMouseLeft.isChecked() == true) {
			tbMouseLeft.setChecked(false);
		}
		
		if(tbMouseRight.isChecked() == true) {
			tbMouseRight.setChecked(false);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(buttonView == tbMouseLeft) {
			if(isChecked == true) {				
				if(tbMouseRight.isChecked() == true) {
					tbMouseRight.setChecked(false);
					
				}
				
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_LEFT, MouseClickMessage.STATE_DOWN));
			
			} else {
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_LEFT, MouseClickMessage.STATE_UP));
			}
			
		} else if(buttonView == tbMouseRight) {
			if(isChecked == true) {
				if(tbMouseLeft.isChecked() == true) {
					tbMouseLeft.setChecked(false);
				}
				
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_RIGHT, MouseClickMessage.STATE_DOWN));
			
			} else {
				MouseControllerActivity.this.application
				.sendMessage(new MouseClickMessage(GlobalVariables.getTemporaryKey(), 
						MouseClickMessage.BUTTON_RIGHT, MouseClickMessage.STATE_UP));
			}
		} 
	}
}
